package com.molichuxing.gateway.utils;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.util.JwtTokenUtil;
import com.molichuxing.framework.contants.RedisKeyConst;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.Md5Util;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.gateway.utils.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户token工具类
 * 
 * @author zoumingyu
 * @date 2019年8月19日
 *
 */
@Component
public class SSOTokenUtil {
	private static final Logger logger = LoggerFactory.getLogger(SSOTokenUtil.class);

	@Value("${token.pwd.salt}")
	private String salt;

	@Value("${sso.token.expiration}")
	private Long expiration;

	@Value("${token.jwt.tokenHead}")
	private String tokenHead;

	/**
	 * head + token生成token
	 * @param account
	 * @return
	 */
	public String generateSsoTokenAndHead(String account){
		String token = generateSsoToken(account);
		return tokenHead + token;
	}

	/**
	 * token生成token
	 * @param account
	 * @return
	 */
	private String generateSsoToken(String account){
		return JwtTokenUtil.generateToken(account, salt, expiration);
	}

	/**
	 * 获取账号
	 * @param token
	 * @return
	 */
	public String getAccountFromSsoToken(String token){
		return getAccount(token.substring(this.tokenHead.length()));
	}

	/**
	 * 获取账号
	 * @param token
	 * @return
	 */
	private String getAccount(String token){
		return JwtTokenUtil.getUserNameFromToken(token, salt);
	}

}
